package com.efounder.util;

import com.core.xml.StubObject;
import com.efounder.constant.EnvironmentVariable;
import com.efounder.service.Registry;

import java.util.List;

/**
 * 图片url动态替换
 * @author lichuan
 * @note 这个类当初是给财务共享项目动态替换头像资源使用的
 */
public class URLModifyDynamic {
    private static List<StubObject> officeRoot;
    private static URLModifyDynamic instance;

    public URLModifyDynamic() {
    }

    public static URLModifyDynamic getInstance() {
        if (instance == null) {
            instance = new URLModifyDynamic();
        }
        if(officeRoot == null) {
            officeRoot = Registry.getRegEntryList("UrlRoot");
            if(officeRoot!=null&&officeRoot.size()>0){
//                for(int i=0;i<officeRoot.size();i++){
//                    StubObject stubObject = officeRoot.get(i);
//                    String id = stubObject.getString("ID","");
//                    String old = stubObject.getString("OLD","");
//                    String newURL = stubObject.getString("NEW","");
//                    hashMap.
//                }
            }

        }
        return instance;
    }

    public  String replace(String url) {
        if(null == url) return "";
        String isNeedControl = EnvironmentVariable.getProperty("IsNeedModifyURL","0");
        if(isNeedControl.equals("1")){
            if(officeRoot!=null&&officeRoot.size()>0){
                for (int i = 0; i <officeRoot.size() ; i++) {
                    StubObject stubObject = officeRoot.get(i);
                    String id = stubObject.getString("ID","");
                    String old = stubObject.getString("OLD","");
                    String newURL = stubObject.getString("NEW","");
                    if(url.contains(old)){
                        url = url.replace(old,newURL);
                        break;
                    }
                }
            }
        }
        return url;
    }
}
